﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightSdms.FacilitiesService;
using System.Collections.Generic;
using SilverlightSdms.VisitsService;
using SilverlightSdms.UserControls.Scheduler;

namespace SilverlightSdms
{
	public partial class UserControlDay : UserControl
	{
        Random m_Random = new Random();
        public int UserId { get; set; }
        public List<VisitInfo> Visits2 { get; set; }
        public List<Visit> Visits { get; private set; }
        public DateTime Date { get; set; }
        private VisitsServiceClient m_VisitClient = new VisitsServiceClient();
        private ChildWindowVisitManager m_ChildWindowVisitManager = new ChildWindowVisitManager();

		public UserControlDay()
		{
			// Required to initialize variables
			InitializeComponent();
            m_VisitClient.GetVisitsByDayCompleted += m_VisitClient_GetVisitsByDayCompleted;
            m_ChildWindowVisitManager.Closed += m_ChildWindowVisitManager_Closed;
		}

        void m_ChildWindowVisitManager_Closed(object sender, EventArgs e)
        {
            Visits = m_ChildWindowVisitManager.Visits;
            UpdateVisitDispaly();
        }

        private void m_VisitClient_GetVisitsByDayCompleted(object sender, GetVisitsByDayCompletedEventArgs e)
        {
            Visits = new List<Visit>((IEnumerable<Visit>)e.Result);
            UpdateVisitDispaly();
        }

        public void SetDate(int DayInMonth, int Month, int Year)
        {
            Date = new DateTime(Year, Month, DayInMonth);
            lblDate.Content = DayInMonth;

            //GetVisits();
            m_VisitClient.GetVisitsByDayAsync(Date, UserId);
        }

        //private void GetVisits()
        //{
        //    if (Date != null)
        //    {
        //        var client = new VisitsServiceClient();

        //        client.GetVisitsCompleted += (s, ea) =>
        //        {
        //            // onComplete check errors!!!
        //            // ea.Error
        //            if (ea.Error == null)
        //            {
        //                Visits = new List<VisitInfo>((IEnumerable<VisitInfo>)ea.Result);
        //                UpdateVisitDispaly();
        //            }
        //        };

        //        client.GetVisitsAsync(((App)Application.Current).UserId, Date);
        //    }
        //}

        public void UpdateVisitDispaly()
        {
            stackPanelAppointements.Children.Clear();
            if (Visits != null && Visits.Count > 0)
            {
                foreach (var visit in Visits)
                {
                    byte[] bytes = new byte[4];

                    m_Random.NextBytes(bytes);
                    Color clr = Color.FromArgb(bytes[0], bytes[1],
                                               bytes[2], bytes[3]);

                    TextBox txtBox = new TextBox();
                    txtBox.Background = new SolidColorBrush(clr);
                    txtBox.Height = ((this.RenderSize.Height / Visits.Count) > 20) ? 20 : (this.RenderSize.Height / Visits.Count);
                    txtBox.DataContext = visit;
                    txtBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("Description"));
                    txtBox.FontSize = ((txtBox.Height / 2) > 3) ? (txtBox.Height / 2) : 1;
                    txtBox.IsReadOnly = true;
                    txtBox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    stackPanelAppointements.Children.Add(txtBox);
                }
            }
        }

        public void ResetDate()
        {
            lblDate.Content = "";
            Visits = null;
            stackPanelAppointements.Children.Clear();
            this.LayoutRoot.Background = new SolidColorBrush();
        }

        public void RefreshData()
        {
            //GetVisits();
            m_VisitClient.GetVisitsByDayAsync(Date, UserId);
        }

        public void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            m_ChildWindowVisitManager.Init(Visits, Date);
            m_ChildWindowVisitManager.UserId = UserId;
            m_ChildWindowVisitManager.Show();
        }
	}
}